package com.iuhao.web.controller;


import com.iuhao.web.core.constants.O2OResponseCode;
import com.iuhao.web.core.exception.ListNULLException;
import com.iuhao.web.core.exception.LoginException;
import com.iuhao.web.core.exception.NULLException;
import com.iuhao.web.core.response.O2OJSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by yanhao on 2017/11/7.
 */
public abstract class BaseController {

    protected Logger log = LoggerFactory.getLogger(this.getClass());

    /**
     * mongoDB  的 MongoTemplate 配置
     */
    //TODO  MongoTemplate 配置
    /*
    @Autowired
    @Qualifier(PrimaryMongoConfig.MONGO_TEMPLATE)
    protected MongoTemplate mongoTemplate;

    @Autowired
    @Qualifier(SecondaryMongoConfig.MONGO_TEMPLATE)
    protected MongoTemplate platformMongoTemplate;
    */

    /**
     * @param e
     * @param json
     */
    public void exception(Exception e, O2OJSON json) {
        if (e instanceof LoginException) {
            json.setError(O2OResponseCode.UNAUTHORIZED, e.getMessage());
        } else if (e instanceof NULLException) {
            json.setSuccessAndNullData();
        } else if (e instanceof ListNULLException) {
            json.setSuccessAndListNullData();
        } else {
            json.setError(O2OResponseCode.DEFAULT_ERROR, e.getMessage());
        }
    }

    protected String getAccessTokenFromRequest(HttpServletRequest request) {
        return !StringUtils.isEmpty(request.getParameter("accessToken")) ? request.getParameter("accessToken") : request.getHeader("accessToken");
    }

}
